<?php
/*
 * 手机客户端专场-取消代理价接口
 * 2014-05-30
 */
include "../inc/public_header.php";
include "../func/user.php";				//用户信息处理函数
include "../func/bid.php";				//拍卖信息处理函数

//常量
$aid 		= $_REQUEST["aid"];		
$uuid 		= $_REQUEST["uuid"];	
$key 		= $_REQUEST["key"];	

//取消代理
if (isset($aid) and isset($uuid) and isset($key)){
	$newkey = md5($aid.$uuid.'lhv5');
	if ($newkey!=$key){
		//KEY不正确
		$rs=array("success"=>"0","errCode"=>"-1","msg"=>"key不正确");
	}else{
		if (strlen($uuid) < 32){
			//"msg"=>"错误的ID"
			$rs=array("success"=>"0","errCode"=>"0","msg"=>"错误的ID");
		}else{
			//当前价格
			$sql = "select lastprice from auction where aid = '".$aid."'";
			$db->query($sql);
			if($db->next_record()){
				$aidprice_lastprice = $db->f("lastprice");
			}
			if(!$aidprice_lastprice){
				//"msg"=>"拍品不存在"
				$rs=array("success"=>"0","errCode"=>"-1","msg"=>"拍品不存在");
			}else{
				//根据uuid查找ID
				$uInfoArr = user_getInfoFromUUID();
				if(!$uInfoArr){
					$rs=array("success"=>"0","price"=>$aidprice_lastprice,"errCode"=>"0","msg"=>"用户ID错误");
				}else{
					$uid = $uInfoArr["uid"];//用户信息主键
					$sql = "SELECT bidproxy,uid from auction_userbidproxy where aid = '".$aid."'";
					$db->query($sql);
					if($db->next_record()){
						$uidaidproxy_bidproxy = $db->f("bidproxy");
						$uidaidproxy_uid = $db->f("uid");
					}
					$userCreditLimit=userCreditLimit($uid);
					//是否领先
					$sql = "select uid,is_bid from auction_userlastbid where aid = '".$aid."' AND uid = '".$uid."'";
					$db->query($sql);
					if($db->next_record()){
						$uidisLead_is_bid = $db->f("is_bid");
						$uidisLead_uid = $db->f("uid");
					}
					$isLead = "0";
					if ($uidisLead_uid){
						$isLead = $uidisLead_is_bid;
					}
					$isProxy = "0";
					$credit = $userCreditLimit;
					if($isLead == "1"){
						//在客户端中，可用额度减去了当前价（不该减去），在此加回来，避免客户端显示错误
						$credit = $credit+$aidprice_lastprice;
					}
					if (!$uidaidproxy_uid){
						//"msg"=>"没有代理价"
						$rs=array("success"=>"1","price"=>$aidprice_lastprice,"isLead"=>$isLead,"isProxy"=>0,"credit"=>$credit,"errCode"=>"0");
					}elseif ($uidaidproxy_uid<>$uid){
						//"msg"=>"你的代理价不存在"
						$rs=array("success"=>"1","price"=>$aidprice_lastprice,"isLead"=>$isLead,"isProxy"=>0,"credit"=>$credit,"errCode"=>"0");
					}else{
						//取消你的代理
						$sql = "DELETE FROM auction_userbidproxy WHERE aid = '".$aid."' and uid = '".$uid."'";
						$db->query($sql);
						//是否领先
						$sql = "select uid,is_bid from auction_userlastbid where aid = '".$aid."' AND uid = '".$uid."'";
						$db->query($sql);
						$userCreditLimit=userCreditLimit($uid);
						if ($uidisLead_uid){
							$isLead = $uidisLead_is_bid;
						}
						if($isLead == "1"){
							//在客户端中，可用额度减去了当前价（不该减去），在此加回来，避免客户端显示错误
							$userCreditLimit = $userCreditLimit+$aidprice_lastprice;
						}
						$rs=array("success"=>"1","price"=>$aidprice_lastprice,"isLead"=>$isLead,"credit"=>$userCreditLimit);
					}
				}
			}
		}
	}
}else{
	//,"msg"=>"少参数"
	$rs=array("success"=>"0","errCode"=>"-1");
}
echo json_encode($rs);
?>
